package com.intel.two.service.record.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.recordMapper.InformationRecordMapper;
import com.intel.two.pojo.record.InformationRecord;
import com.intel.two.service.record.InformationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InformationRecordServiceImpl implements InformationRecordService {
    @Autowired
    private InformationRecordMapper informationRecordMapper;
    @Override
    public LayuiPageVo<InformationRecord> selectAllRecord(Map<String,Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<InformationRecord> informationRecords =  informationRecordMapper.selectAllRecord(params);

        // mybatis 的分页数据结构
        PageInfo<InformationRecord> recordPageInfo = new PageInfo<>(informationRecords);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<InformationRecord> recordLayuiPageVo = new LayuiPageVo<>();
        recordLayuiPageVo.setCode(0);
        recordLayuiPageVo.setMsg("居民分页列表数据");
        recordLayuiPageVo.setCount(recordPageInfo.getTotal());  // 总记录数
        recordLayuiPageVo.setData(recordPageInfo.getList());  // 分页列表数据

        return recordLayuiPageVo;

    }

    @Override
    public int recordAdd(InformationRecord informationRecord) {


        return informationRecordMapper.recordAdd(informationRecord);
    }

    @Override
    public int deleteInformationRecord(Integer id) {
        return informationRecordMapper.deleteInformationRecord(id);
    }

    @Override
    public int deleteInformationRecordList(String ids) {
        String[] split = ids.split(",");
        Integer[] id = new Integer[split.length];
        int count=0;
        for (String s : split) {
            Integer integer = Integer.valueOf(s);
            id[count]=integer;
            count++;

        }
        return informationRecordMapper.deleteInformationRecordList(id);
    }

    @Override
    public int update(InformationRecord informationRecord) {

        return informationRecordMapper.update(informationRecord);
    }
}
